package com.cosmoplat.gfqd.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class MavenExecutor {


    public static void execute(File directory, String commandStr) {
        String os = System.getProperty("os.name").toLowerCase();
        String[] command;

        if (os.contains("win")) {
            // Windows command
            command = new String[]{"cmd.exe", "/c", commandStr};
        } else {
            // Linux/Unix command
            command = new String[]{"/bin/bash", "-c", commandStr};
        }

        ProcessBuilder processBuilder = new ProcessBuilder(command);
        processBuilder.directory(directory);  // Set the working directory
        processBuilder.redirectErrorStream(true);

        try {
            Process process = processBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;

            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            int exitCode = process.waitFor();
            System.out.println("Maven command exited with code: " + exitCode);

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
